package com.example.demo.mapper;

import com.example.demo.mapper.entity.Commodity;
import com.example.demo.mapper.entity.ExchangeRecord;
import com.example.demo.mapper.entity.StudentPoints;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Lucy
 * @create 2024-02-13 16:07
 */
@Mapper
@Repository
public interface StudentPointsMapper {
    @Insert("INSERT INTO student_points(student_id) " +
            "   VALUES(#{studentId})")
    @Options(useGeneratedKeys = true, keyProperty = "studentId", keyColumn = "student_id")
    void insertStudentPoints(StudentPoints points);
    @Select("SELECT * FROM student_points WHERE student_id = #{studentId}")
    StudentPoints selectStudentPointsByStudentId(int studentId);
    @Select("SELECT * FROM commodity WHERE category = #{type} AND is_offline = 1")
    List<Commodity> selectTypeCommodityList(int type);
    @Select("SELECT * FROM commodity WHERE commodity_id = #{commodityId}")
    Commodity selectCommodityById(int commodityId);
    @Update("UPDATE commodity SET stock = stock - 1, version = version + 1 WHERE commodity_id = #{commodityId} AND version = #{currentVersion}")
    int deductStock(@Param("commodityId") int commodityId, @Param("currentVersion") int currentVersion);
    @Update("UPDATE student_points SET points = #{points}, total_earned = #{totalEarned}, total_spent = #{totalSpent}, last_updated = #{lastUpdated} WHERE student_id = #{studentId}")
    void updateStudentPoints(StudentPoints studentPoints);
    @Insert("INSERT INTO exchange_record (student_id, commodity_id, points_used) " +
            "VALUES (#{studentId}, #{commodityId}, #{pointsUsed})")
    @Options(useGeneratedKeys = true, keyProperty = "recordId", keyColumn = "record_id")
    void insertExchangeRecord(ExchangeRecord exchangeRecord);
    @Update("UPDATE commodity SET is_offline = 3 WHERE commodity_id = #{commodityId}")
    void updateIsOffline(int commodityId);
    @Select("SELECT * FROM student_points")
    List<StudentPoints> selectStudentPointsList();
}
